﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using Xpom_Xpum__Code_Generator.Utilities;

namespace Xpom_Xpum__Code_Generator.Core
{
    class ProjectClass
    {
        public ManifestClass ManifestObject { get; set; }

        private string ProjectPath = "";

        public ProjectClass(string path, string templatePath)
        {
            // create project directory
            Directory.CreateDirectory(path);
            
            // copy template files to project directory
            FileSystem.CopyDirectoryContent(templatePath + @"Files\", path);
            
            // set manifest default values
            if (File.Exists(path + "Manifest.json"))
            {
                File.WriteAllText(path + "Manifest.json",
                    File.ReadAllText(path + "Manifest.json").Replace("__PARAM_manifest_version__", "2"));

                File.WriteAllText(path + "Manifest.json",
                    File.ReadAllText(path + "Manifest.json").Replace("__PARAM_version__", "1.0"));

                File.WriteAllText(path + "Manifest.json",
                    File.ReadAllText(path + "Manifest.json").Replace("__PARAM_name__", Path.GetFileName(Path.GetDirectoryName(path))));
            }

            // manifest initialization
            if (File.Exists(path + "Manifest.json"))
            {
                ManifestObject = ManifestClass.Parse(File.ReadAllText(path + "Manifest.json"));
            }

            // other initialization
            ProjectPath = path;
        }

        public void Save()
        {
            ManifestObject.Save(ProjectPath + "Manifest.json");
        }

        public string ProjectName
        {
            get
            {
                return ManifestObject.name;
            }
            set
            {
                ManifestObject.name = value;
            }
        }
    }
}
